package main.java.com.lee.juc.aqs;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.stream.IntStream;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/2/14
 */
public class SimpleAQSLockTest {
    static int sum = 0;
    public static void main(String[] args) throws InterruptedException {
        SimpleAQSLock lock = new SimpleAQSLock();
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        IntStream.rangeClosed(1, 100).forEach(i->{
            executorService.submit(() -> sum(lock));
        });
        executorService.shutdown();
        while (!executorService.isTerminated()) {
//            System.out.println("任务未结束");
        }
        System.out.println(sum);
    }

    private static void sum(Lock lock) {
        lock.lock();
        try {
            IntStream.rangeClosed(1, 100).forEach(i -> sum = sum + 1);
        }finally {
            lock.unlock();
        }
    }
}
